<?php

class  Model_ZendDb_BoxService implements Model_BoxService 
{
    /**
     * @var Zend_Db_Adapter_Abstract
     */
    private $_db;
    
    /**
     * @var Model_ZendDb_DbTable_Feed
     */
    private $_feed;
    
    /**
     * @var Model_ZendDb_DbTable_Homepagebox
     */
    private $_box;
    
    public function __construct()
    {
        $this->_feed = new Model_ZendDb_DbTable_Feed();
        $this->_db = $this->_feed->getAdapter();
        $this->_box = new Model_ZendDb_DbTable_Homepagebox();
    }

    /**
     * Search for boxes matching search criteria based on published status and
     * contents of name and url fields of feed and box
     * If no criteria are specified it will return the entire list of Boxes
     * in the system
     * 
     * @param array $criteria an optional associative array 
     * @return Zend_Paginator_Adapter_Interface
     * @see application/models/Model_BoxService#searchBoxes($creitria)
     */
	public function searchBoxes($criteria = null) {
        $select = $this->_db->select();
        $select->from('homepagebox')
               ->join('feed', 'homepagebox.feed_oid = feed.oid', null);
        
        if ($criteria !== null) {
            if (isset($criteria['isPublished']) && 
                is_numeric($criteria['isPublished'])) {
                	
                $select->where('homepagebox.isPublished = ?', 
                                $criteria['isPublished']);
            }
            if (isset($criteria['contains']) && !empty($criteria['contains'])) {
                $contains = '%'.$criteria['contains'].'%';
                
                $select->where("UPPER(homepagebox.title) like UPPER('$contains') or ".
                               "UPPER(feed.name) like UPPER('$contains') or ".
                               "UPPER(feed.url) like UPPER('$contains') or ".
                               "UPPER(feed.description) like UPPER('$contains')"); 
            }
        }
        
        //die($select->__toString());
        return new Model_ZendDb_Paginator_SearchBox($select);		
	}
	
	/**
     * Get information of box wich id is $boxID
     * 
     * @param int $boxID
     * @return Model_Entity_Homepagebox
	 * @see application/models/Model_BoxService#getBox($boxID)
	 */
	public function getBox($boxID) {
		$result = $this->_box->find($boxID);
		
		$box = null;
        
        if (! 0 == count($result)) {
        	$feedData = $result->current()
        	                   ->findParentRow('Model_ZendDb_DbTable_Feed');
                              
            $box = new Model_Entity_Homepagebox($result->current()->toArray());
            $feed = new Model_Entity_Feed($feedData->toArray());
            $box->setFeed($feed);
        }
        
        return $box;
	}

	/**
     * Delete an existing box
     * 
     * @param int $boxID
     * @return void
	 * @see application/models/Model_BoxService#deleteBox($boxID)
	 */
	public function deleteBox($boxID) {
		$this->_box->delete(array('oid = ?' => $boxID));
	}

	/**
     * Add a new box
     * 
     * @param Model_Entity_Homepagebox $box
     * @return void
	 * @see application/models/Model_BoxService#addBox($box)
	 */
	public function addBox($box) {
		$boxData = $box->getOptions();
        
		unset($boxData['oid']);
		unset($boxData['feed']);
		
		$boxData['feed_oid'] = $box->getFeed()->getOid();
		
		try {
			$this->_db->beginTransaction();
			
			$this->_box->insert($boxData);
			
			$this->_db->commit();
		} catch (Exception $e) {
			$this->_db->rollBack();
            
            throw $e;
		}
	}

	/**
     * Update box information
     * 
     * @param Model_Entity_Homepagebox $box
     * @return void
	 * @see application/models/Model_BoxService#editBox($box)
	 */
	public function editBox($box) {
		$boxData = $box->getOptions();
		
		unset($boxData['feed']);
		
		$boxData['feed_oid'] = $box->getFeed()->getOid();
		$this->_category->update($boxData, 
              array('oid = ?' => $boxData['oid']));
	}
	
	
    /**
     * Published/Not Published box
     * 
     * @param Model_Entity_Homepagebox $box
     * @return void
     * @see application/models/Model_BoxService#setPublished($box)
     */
    public function setPublished($box) {
        $published['isPublished'] = $box->getIsPublished();
        
        $val= $this->_box->update($published,
                             array('oid = ?' => $box->getOid()));
    }
}
